<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>Catalyst 的用户鉴定登陆</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="URI_Find.html">在文本中寻找 URI 地址</a>&nbsp;&nbsp;>>Next: <a href="DBIx_Class_inflate.html">DBIx::Class 的 inflate_column</a></p>
<h1>Catalyst 的用户鉴定登陆</h1>
<div class='content'>
<p>Category: <a href='Catalyst.html'>Catalyst</a> &nbsp; Keywords: <b>Authentication Catalyst</b></p>因为最近开始有点闲下来（当然，虽说闲下来也有很多事情要做，比如毕业论文，实习，part-time job 等），于是在刚过去的一个多小时里开始了一直想写的基于 <a href="http://dev.catalyst.perl.org">Catalyst</a> 的论坛程序。坦白说，Catalyst 我也有很多地方都不懂，都没试验过。因为要写论坛程序，类似 Session, Authentication 这样的东西是必不可少，于是就试了下 Catalyst 的 Authentication 插件。<p />写个详细的流程，方便诸位再次试验。<br /><ol><li>首先当然是创建整个项目的结构：（过程部分略）<br /><pre>catalsyt Foorum<br />cd Foorum<br />perl script/foorum_create.pl controller User<br />perl script/foorum_create.pl view <a href="http://www.template-toolkit.org">TT</a> TT<br />perl script/foorum_create.pl model DBIC DBIC<br />..</pre><li>创建数据库 foorum, 创建表 user, 表的结构其他不管，两个字段是必须的，username 和 password<br /><li>修改文件：<br /><ul><li>Foorum.pm<br /><pre>package Foorum;<p />use strict;<br />use warnings;<p />use Catalyst qw/<br /> &nbsp; &nbsp;-Debug<br /> &nbsp; &nbsp;ConfigLoader<br /> &nbsp; &nbsp;Authentication<br /> &nbsp; &nbsp;Authentication::Store::DBIC<br /> &nbsp; &nbsp;Authentication::Credential::Password<br /> &nbsp; &nbsp;Session<br /> &nbsp; &nbsp;Session::Store::File<br /> &nbsp; &nbsp;Session::State::Cookie<br /> &nbsp; &nbsp;Static::Simple/;<p />our $VERSION = '0.01';<p />__PACKAGE__->setup;<p />sub default : Private {<br />    my ( $self, $c ) = @_;<p />    # Hello World<br />    $c->response->body( $c->welcome_message );<br />}</pre>因为在 Win32 下跑，所以 <a href="http://search.cpan.org/perldoc?Catalyst::Plugin::Session::Store::FastMmap">Catalyst::Plugin::Session::Store::FastMmap</a> 是安装不起来的，而我试了下 <a href="http://search.cpan.org/perldoc?Catalyst::Plugin::Session::Store::DBI">Catalyst::Plugin::Session::Store::DBI</a> 发现报错，最后只好转为 <a href="http://search.cpan.org/perldoc?Catalyst::Plugin::Session::Store::File">Catalyst::Plugin::Session::Store::File</a> .<br />用 File 模块，于是在与 lib script 同级目录下创建了个文件夹 sessions<br /><li>foorum.yml<br /><pre>---<br />name: Foorum<br />dsn: dbi:mysql:foorum<br />dsn_user: root<br />dsn_pwd: ''<p />authentication:<br />  dbic:<br />    user_class: "Foorum::Model::DBIC::User"<br />    user_field: "username"<br />    password_field: "password"<br />    password_type: "hashed"<br />    password_hash_type: "SHA-1"<br />session:<br />  expires: 3600<br />  storage: __HOME__/sessions<br /></pre>大致看看并会明白。密码用 SHA-1 加密。<br /><li>修改 Model/DBIC.pm<br /><pre>package Foorum::Model::DBIC;<p />use strict;<br />use base 'Catalyst::Model::DBIC';<p />__PACKAGE__->config(<br />    dsn           => Foorum->config->{dsn},<br />    password      => Foorum->config->{dsn_pwd},<br />    user          => Foorum->config->{dsn_user},<br />    options       => { AutoCommit => 1, RaiseError => 1, PrintError => 1 },<br />    relationships => 1,<br />);<p />1;</pre><li>创建 Model/DBIC/User.pm<br /><pre>package Foorum::Model::DBIC::User;<p />use strict;<br />use base 'Foorum::Model::DBIC';<p />1;</pre></ul><li>上面大致就是所有的准备工作了。接下来就是对 Foorum::Controller::User 做一些动作了。<br />因为是测试，所以我们先增加了一条纪录。<br /><pre>package Foorum::Controller::User;<p />use strict;<br />use warnings;<br />use base 'Catalyst::Controller';<p />sub insert : Local {<br /> &nbsp; &nbsp;my ( $self, $c ) = @_;<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;use Digest ();<br /> &nbsp; &nbsp;my $password = '123456';<p /> &nbsp; &nbsp;my $d = Digest->new( 'SHA-1' );<br /> &nbsp; &nbsp;$d->add($password);<br /> &nbsp; &nbsp;my $computed = $d->digest;<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;$c->model('DBIC')->table('user')->create({ <br />     &nbsp; &nbsp;username  => 'fayland',<br />     &nbsp; &nbsp;password  => $computed<br /> &nbsp; &nbsp;});<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;$c->res->body('hi, add test user name to table.');<br />}</pre>而 Authentication 的验证过程就开始变得简单了。因为仅仅是试验，所以就用了最简单的代码。<br /><pre>sub login : Local {<br /> &nbsp; &nbsp;my ( $self, $c ) = @_;<br /> &nbsp; &nbsp;<br /> &nbsp; &nbsp;if ( $c->login('fayland', '123456') ) {<br /> &nbsp; &nbsp; &nbsp; &nbsp;$c->res->body("hello, " . $c->user->username);<br /> &nbsp; &nbsp;} else {<br /> &nbsp; &nbsp; &nbsp; &nbsp;$c->res->body('failed');<br /> &nbsp; &nbsp;}<br />}</pre>大致并是如是，运行后先访问 http://fayland:3000/user/insert 来插入数据，然后运行 http://fayland:3000/user/login 来做测试。如果是 123456 屏幕就会输出 hello, fayland，不是的话就会输出 failed.<br /></ol><p />上面并是所有的大致过程。详细的查阅 <a href="http://search.cpan.org/perldoc?Catalyst::Plugin::Authentication">Catalyst::Plugin::Authentication</a> 或者等我继续。我还要继续写 Foorum 代码，或许过几天就会再次讲到详细的应用。have fun!</div>
<p><&lt;Previous: <a href="URI_Find.html">在文本中寻找 URI 地址</a>&nbsp;&nbsp;>>Next: <a href="DBIx_Class_inflate.html">DBIx::Class 的 inflate_column</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=Catalyst%20%E7%9A%84%E7%94%A8%E6%88%B7%E9%89%B4%E5%AE%9A%E7%99%BB%E9%99%86&url=http://www.fayland.org/journal/Catalyst_Authentication.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='050328.html'>Catalyst & Eplanet</a> < <span class='digit'>2005-03-28 13:19:19</span> ></li><li><a href='050423.html'>Day [05.4.23] stay with me</a> < <span class='digit'>2005-04-23 16:17:02</span> ></li><li><a href='Catalyst_Authenticate.html'>Catalyst Authenticate</a> < <span class='digit'>2005-09-26 12:50:39</span> ></li><li><a href='Catalyst_Flaw.html'>Catalyst 的一个不足（一个已去掉）</a> < <span class='digit'>2005-09-27 13:44:31</span> ></li><li><a href='Catalyst_Session_Win32.html'>Catalyst 在 Win32 下的 Session</a> < <span class='digit'>2005-09-29 11:08:04</span> ></li><li><a href='Catalyst_NoFavIcon.html'>Catalyst && favicon.ico</a> < <span class='digit'>2005-09-30 00:57:57</span> ></li><li><a href='Catalyst_Overview.html'>我对 Catalyst 的理解和介绍</a> < <span class='digit'>2005-10-08 01:39:40</span> ></li><li><a href='Catalyst_XMLRPC.html'>Catalyst && XML-RPC</a> < <span class='digit'>2005-10-11 21:50:30</span> ></li><li><a href='Catalyst_YAML.html'>Catalyst config YAML</a> < <span class='digit'>2005-12-10 12:50:39</span> ></li><li><a href='CatalystAdventCalendar_CN.html'>Catalyst Advent Calendar 中文版</a> < <span class='digit'>2005-12-15 23:28:40</span> ></li></ul>
Created on <span class="digit">2006-02-23 18:40:03</span>, Last modified on <span class="digit">2006-02-23 18:42:08</span><br />
Copyright 2004-2006 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.65.
</body>
</html>